package com.ruoyi.internet.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.internet.api.domain.IntUser;
import com.ruoyi.internet.api.enums.IntUserEnum;
import com.ruoyi.internet.domain.IntMaterial;
import com.ruoyi.internet.mapper.IntMaterialMapper;
import com.ruoyi.internet.mapper.IntUserMapper;
import com.ruoyi.internet.service.IIntMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 原材料采购Service业务层处理
 *
 * @author cjie
 * @date 2021-08-16
 */
@Service
public class IntMaterialServiceImpl implements IIntMaterialService {
    @Autowired
    private IntMaterialMapper intMaterialMapper;
    @Autowired
    private IntUserMapper intUserMapper;

    /**
     * 查询原材料采购
     *
     * @param id 原材料采购ID
     * @return 原材料采购
     */
    @Override
    public IntMaterial selectIntMaterialById(String id) {
        IntMaterial intMaterial = intMaterialMapper.selectIntMaterialById(id);
        IntUser intUser = intUserMapper.selectIntUserById(intMaterial.getExpirationUserId());
        if(IntUserEnum.企业交易商.getCode().equals(intUser.getType())){
            intMaterial.setExpirationName(intUser.getEnterpriseName());
        }
        intMaterial.setType(intUser.getType());
        //未登录，去掉联系人和电话
        if (StringUtils.isEmpty(SecurityUtils.getUsername())) {
            intMaterial.setLinkMan(null);
            intMaterial.setPhone(null);
        }
        if(StringUtils.isNotEmpty(intMaterial.getQualityPic())){
            intMaterial.setQualityPicArray(JSON.parseArray(intMaterial.getQualityPic()));
        }
        if(StringUtils.isNotEmpty(intMaterial.getGoodsPic())){
            intMaterial.setGoodsPicArray(JSON.parseArray(intMaterial.getGoodsPic()));
        }
        return intMaterial;
    }

    /**
     * 查询原材料采购列表
     *
     * @param intMaterial 原材料采购
     * @return 原材料采购
     */
    @Override
    public List<IntMaterial> selectIntMaterialList(IntMaterial intMaterial) {
        List<IntMaterial> intMaterials = intMaterialMapper.selectIntMaterialList(intMaterial);
        intMaterials.forEach(material->{
            if(StringUtils.isNotEmpty(material.getQualityPic())){
                material.setQualityPicArray(JSON.parseArray(material.getQualityPic()));
            }
            if(StringUtils.isNotEmpty(material.getGoodsPic())){
                material.setGoodsPicArray(JSON.parseArray(material.getGoodsPic()));
            }
        });
        return intMaterials;
    }

    /**
     * 新增原材料采购
     *
     * @param intMaterial 原材料采购
     * @return 结果
     */
    @Override
    public int insertIntMaterial(IntMaterial intMaterial) {
        intMaterial.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        intMaterial.setCreateTime(DateUtils.getNowDate());
        intMaterial.setCreateBy(SecurityUtils.getUsername());
        intMaterial.setExpirationUserId(SecurityUtils.getUserId());

        if(intMaterial.getQualityPicArray() != null){
            intMaterial.setQualityPic(JSON.toJSONString(intMaterial.getQualityPicArray()));
        }
        if(intMaterial.getGoodsPicArray() != null){
            intMaterial.setGoodsPic(JSON.toJSONString(intMaterial.getGoodsPicArray()));
        }
        return intMaterialMapper.insertIntMaterial(intMaterial);
    }

    /**
     * 修改原材料采购
     *
     * @param intMaterial 原材料采购
     * @return 结果
     */
    @Override
    public int updateIntMaterial(IntMaterial intMaterial) {
        intMaterial.setUpdateTime(DateUtils.getNowDate());
        intMaterial.setUpdateBy(SecurityUtils.getUsername());

        if(intMaterial.getQualityPicArray() != null){
            intMaterial.setQualityPic(JSON.toJSONString(intMaterial.getQualityPicArray()));
        }
        if(intMaterial.getGoodsPicArray() != null){
            intMaterial.setGoodsPic(JSON.toJSONString(intMaterial.getGoodsPicArray()));
        }
        return intMaterialMapper.updateIntMaterial(intMaterial);
    }

    /**
     * 批量删除原材料采购
     *
     * @param ids 需要删除的原材料采购ID
     * @return 结果
     */
    @Override
    public int deleteIntMaterialByIds(String[] ids) {
        return intMaterialMapper.deleteIntMaterialByIds(ids);
    }

    /**
     * 删除原材料采购信息
     *
     * @param id 原材料采购ID
     * @return 结果
     */
    @Override
    public int deleteIntMaterialById(String id) {
        return intMaterialMapper.deleteIntMaterialById(id);
    }
}
